package com.toman.Adpter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.toman.Adpter.holder.TallyHolder;
import com.toman.Enum.BaseEnum;
import com.toman.Enum.PermEnum;
import com.toman.JavaBean.DeviceCheckRecordEntity;

import com.toman.R;
import com.toman.Util.PermissonUtils;
import com.toman.Util.SPUtils;

import java.util.List;

/**
 * Created by 邹苏隆 on 2018/6/19.
 * description：
 */

public class TallyAdapter extends AdapterBase<DeviceCheckRecordEntity,TallyHolder> implements View.OnClickListener ,CompoundButton.OnCheckedChangeListener {




    public interface ActionClickListener {
        public void onDetailClick(DeviceCheckRecordEntity bean);
//查看详情
        public void onDelClick(DeviceCheckRecordEntity bean);
//删除
        public void onCheckClick(DeviceCheckRecordEntity bean);
//审核
    }


    private ActionClickListener actionClickListener;
    public void setActionClickListener(ActionClickListener l) {
        actionClickListener = l;
    }

    private LayoutInflater inflater;
    private Context mContext;
    private int type;

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public TallyAdapter(Context context) {
        inflater = LayoutInflater.from(context);
        this.mContext = context;
        type=1;
    }

    @Override

    protected void handlerData(List<DeviceCheckRecordEntity> mList2, int position, TallyHolder itemView) {
        DeviceCheckRecordEntity bean = mList2.get(position);
switch (type){
    case 1:
        itemView.ll_1.setVisibility(View.GONE);
        itemView.ll_2.setVisibility(View.VISIBLE);
        break;
    case 2:
        itemView.ll_2.setVisibility(View.GONE);
        itemView.ll_1.setVisibility(View.VISIBLE);
        break;
}
//待点检
        if (type==1){
           ;

            itemView.tv_name2.setText(bean.getDeviceName());
            itemView.tv_no2.setText("("+bean.getDeviceEntity().getDeviceCode()+")");
            itemView.cb_status2.setTag(position);
            itemView.cb_status2.setChecked(bean.isSelect_status());
            if (bean.isOutOfTime()){
                itemView.cb_status2.setVisibility(View.INVISIBLE);
                itemView.tv_name2.setTextColor(mContext.getResources().getColor(R.color.black99));
                itemView.tv_no2.setTextColor(mContext.getResources().getColor(R.color.black99));
            }
            else {
                itemView.tv_name2.setTextColor(mContext.getResources().getColor(R.color.button_blue));
                itemView.tv_no2.setTextColor(mContext.getResources().getColor(R.color.button_blue));
                itemView.cb_status2.setOnCheckedChangeListener(this);
                itemView.cb_status2.setVisibility(View.VISIBLE);
            }

        }
        else {
//待审核
            if (bean.getPassAllIf().equals("0")){
                itemView.iv_status.setImageResource(R.mipmap.icon_tg);
            }
            else {
                itemView.iv_status.setImageResource(R.mipmap.icon_btg);
            }

            itemView.tv_name.setText(bean.getDeviceName());
            itemView.tv_shift.setText(bean.getShiftName());
            itemView.tv_dep.setText(bean.getDeviceEntity().getDepartmentEntity().getDepartmentName());
            itemView.tv_no.setText("("+bean.getDeviceEntity().getDeviceCode()+")");
            itemView.cb_status.setChecked(bean.isSelect_status());
            if (bean.getCheckStatus().equals("1")){
                itemView.tv_time.setText(bean.getCreateTime());
                try {
                    itemView.tv_tally.setText(bean.getCheckUser().getRealName());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (PermissonUtils.contains(mContext, BaseEnum.TALLY.has(PermEnum.DELETE))){
                itemView.btn_del.setVisibility(View.VISIBLE);}
                else {
                    itemView.btn_del.setVisibility(View.GONE);
                }
                itemView.tv_tally_name.setText("点检人员");
//                if (!PermissonUtils.contains(mContext,"点检"))
                if (PermissonUtils.contains(mContext, BaseEnum.TALLY.has(PermEnum.CHECK))){
                itemView.btn_check.setVisibility(View.VISIBLE);
                itemView.cb_status.setVisibility(View.VISIBLE);}
                else {
                    itemView.btn_check.setVisibility(View.GONE);
                    itemView.cb_status.setVisibility(View.GONE);
                }
//            获取选中人员

            }
            //            已审核
            else{
                try {
                    if (PermissonUtils.contains(mContext, BaseEnum.TALLY.has(PermEnum.DELETE))){
                        itemView.btn_del.setVisibility(View.VISIBLE);}
                    else {
                        itemView.btn_del.setVisibility(View.GONE);
                    }                    itemView.btn_check.setVisibility(View.GONE);
                    itemView.tv_time.setText(bean.getAuditTime());
                    itemView.tv_tally_name.setText("审核人员");
                    itemView.tv_tally.setText(bean.getAuditUser().getRealName());
                    itemView.cb_status.setVisibility(View.GONE);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
            itemView.cb_status.setOnCheckedChangeListener(this);
            itemView.cb_status.setTag(position);

            itemView.btn_del.setOnClickListener(this);
            itemView.btn_del.setTag(position);
            itemView.btn_check.setOnClickListener(this);
            itemView.btn_check.setTag(position);
            itemView.btn_detail.setOnClickListener(this);
            itemView.btn_detail.setTag(position);

        }

    }

    @Override
    protected TallyHolder getItemViewHolder() {

        return new TallyHolder();
    }

    @Override
    protected View initConvertView(View convertView, TallyHolder v) {

    convertView = inflater.inflate(R.layout.item_dianjian, null);
    v.iv_status= (ImageView) convertView.findViewById(R.id.iv_status);
    v.btn_check= (Button) convertView.findViewById(R.id.btn_check);
    v.btn_del= (Button) convertView.findViewById(R.id.btn_del);
    v.btn_detail= (Button) convertView.findViewById(R.id.btn_detail);
    v.tv_name= (TextView) convertView.findViewById(R.id.tv_name);
    v.tv_tally_name = (TextView) convertView.findViewById(R.id.tv_tally_title);
    v.tv_dep= (TextView) convertView.findViewById(R.id.tv_dep);
    v.tv_no= (TextView) convertView.findViewById(R.id.tv_no);
    v.tv_shift= (TextView) convertView.findViewById(R.id.tv_shift);
    v.tv_tally= (TextView) convertView.findViewById(R.id.tv_tally);
    v.tv_time= (TextView) convertView.findViewById(R.id.tv_time);
    v.cb_status=(CheckBox) convertView.findViewById(R.id.cb_status);
        v.tv_name2= (TextView) convertView.findViewById(R.id.tv_name2);
        v.tv_no2= (TextView) convertView.findViewById(R.id.tv_no2);
        v.cb_status2=(CheckBox) convertView.findViewById(R.id.cb_status2);
        v.ll_1=(LinearLayout) convertView.findViewById(R.id.ll_1);
        v.ll_2=(LinearLayout) convertView.findViewById(R.id.ll_2);

        return convertView;


    }



    @Override
    protected void onReachBottom() {

    }

    @Override
    public void onClick(View view) {
        int position = (Integer) view.getTag();
        DeviceCheckRecordEntity bean = getItem(position);

        switch (view.getId()) {
            case R.id.btn_detail:
                if (actionClickListener != null) {
                    actionClickListener.onDetailClick(bean);
                }
                break;
            case R.id.btn_del:
                if (actionClickListener != null) {
                    actionClickListener.onDelClick(bean);
                }
                break;
            case R.id.btn_check:
                if (actionClickListener != null) {
                    actionClickListener.onCheckClick(bean);
                }
                break;

        }
    }
    @Override
    public void onCheckedChanged(CompoundButton view, boolean b) {
        try {
            int position = (Integer) view.getTag();
            DeviceCheckRecordEntity bean = getItem(position);

         bean.setSelect_status(b);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
